package temp.lho.lws.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import temp.lho.lws.emum.MsgStatusEmum;
import temp.lho.lws.pojo.Flowinfomation;
import temp.lho.lws.pojo.SurveyLossInfo;
import temp.lho.lws.result.ResultBody;
import temp.lho.lws.service.IFlowinfomationService;
import temp.lho.lws.service.ISurveyLossInfoService;
import temp.lho.lws.status.FlowStatus;
import temp.lho.lws.vo.LoginVo;

import javax.servlet.http.HttpServletRequest;

/**
 * (SurveyLossInfo)表控制层
 *
 * @author makejava
 * @since 2020-07-06 23:08:54
 */
@RestController
@RequestMapping("/surveyLossInfo")
public class SurveyLossInfoController {
    /**
     * 服务对象
     */
    @Autowired
    private ISurveyLossInfoService surveyLossInfoService;

    @Autowired
    private IFlowinfomationService flowinfomationService;


    @GetMapping("/findTempInfo")
    public ResultBody findTempInfo(Integer claimMainId) {
        SurveyLossInfo byClaimMainId = surveyLossInfoService.findByClaimMainId(claimMainId);
        return byClaimMainId == null ? ResultBody.toResultBody(-1,"") : ResultBody.toResultBody(200,"",byClaimMainId);
    }

    /**
     * 暂存勘察
     * @param
     * @return
     */
    @PostMapping("/tempSave")
    public ResultBody tempSave(@RequestBody SurveyLossInfo surveyLossInfo, HttpServletRequest request) {

        if (surveyLossInfo.getIseffectivedrivinglicence() == null) {
            surveyLossInfo.setIseffectivedrivinglicence(0);
        }
        if (surveyLossInfo.getIseffectivevehiclelicence() == null) {
            surveyLossInfo.setIseffectivevehiclelicence(0);
        }
        if (surveyLossInfo.getIsviolationload() == null) {
            surveyLossInfo.setIsviolationload(0);
        }
        if (surveyLossInfo.getIsoversteptravelregion() == null) {
            surveyLossInfo.setIsoversteptravelregion(0);
        }
        if (surveyLossInfo.getIsloaddangerousgoods() == null) {
            surveyLossInfo.setIsloaddangerousgoods(0);
        }
        if (surveyLossInfo.getIsbeforedangerdefect() == null) {
            surveyLossInfo.setIsbeforedangerdefect(0);
        }

        //获取登陆用户
        LoginVo user = (LoginVo) request.getSession().getAttribute("user");

        //添加查勘表信息
        int add = surveyLossInfoService.add(surveyLossInfo);
        boolean flag = add > 0;
        if (flag) {
            flag = flowinfomationService.updateJobStatus(surveyLossInfo.getTaskid(), FlowStatus.FLOW_JOB_TEMPSAVE,null,user);
        }

        return flag ? ResultBody.toResultBody(MsgStatusEmum.SAVE_SUCCESS.getCode(),MsgStatusEmum.SAVE_SUCCESS.getMsg()) : ResultBody.toResultBody(MsgStatusEmum.SAVE_SUCCESS.getCode(),MsgStatusEmum.SAVE_ERROR.getMsg());
    }

    /**
     * 提交
     * @param
     * @return
     */
    @PostMapping("/commitSurvey")
    public ResultBody commitSurvey(@RequestBody SurveyLossInfo surveyLossInfo , HttpServletRequest request) {

        if (surveyLossInfo.getIseffectivedrivinglicence() == null) {
            surveyLossInfo.setIseffectivedrivinglicence(0);
        }
        if (surveyLossInfo.getIseffectivevehiclelicence() == null) {
            surveyLossInfo.setIseffectivevehiclelicence(0);
        }
        if (surveyLossInfo.getIsviolationload() == null) {
            surveyLossInfo.setIsviolationload(0);
        }
        if (surveyLossInfo.getIsoversteptravelregion() == null) {
            surveyLossInfo.setIsoversteptravelregion(0);
        }
        if (surveyLossInfo.getIsloaddangerousgoods() == null) {
            surveyLossInfo.setIsloaddangerousgoods(0);
        }
        if (surveyLossInfo.getIsbeforedangerdefect() == null) {
            surveyLossInfo.setIsbeforedangerdefect(0);
        }

        //添加查勘表信息
        surveyLossInfoService.add(surveyLossInfo);

        //获取用户信息
        LoginVo user = (LoginVo) request.getSession().getAttribute("user");

        //查询所属父流程
        Flowinfomation byTaskId = flowinfomationService.findByTaskId(surveyLossInfo.getTaskid());
        //创建要添加的下一步流程对象
        Flowinfomation flowinfomation = new Flowinfomation();
        flowinfomation.setTaskpid(byTaskId.getTaskid());
        flowinfomation.setTaskname(FlowStatus.FLOW_VERIFY);
        flowinfomation.setFlowid(byTaskId.getFlowid());
        flowinfomation.setClaimmainid(byTaskId.getClaimmainid());
        flowinfomation.setAssignedid(user.getUserId().toString());
        flowinfomation.setAssignedname(user.getUsername());

        //添加下一步流程
        boolean flag = flowinfomationService.addNextFlow(flowinfomation);
        if (flag) {
            flag = flowinfomationService.updateJobStatus(byTaskId.getTaskid(), FlowStatus.FLOW_JOB_OK,null, user);
        }

        return flag ? ResultBody.toResultBody(
                MsgStatusEmum.COMMIT_SUCCESS.getCode(),
                MsgStatusEmum.COMMIT_SUCCESS.getMsg())
                : ResultBody.toResultBody(
                MsgStatusEmum.COMMIT_ERROR.getCode(),
                MsgStatusEmum.COMMIT_ERROR.getMsg());
    }

}